#include "../include/ticket.h"
#include "../include/longin.h"

//购买车票                
int  buyTicket(TRAIN *head, PASS *head1)
{
    assert(head);
    assert(head1);
    TRAIN *end = NULL;
    end = head->next;

    PASS *end1 = NULL;
    end1 = head1->next;
    char trips[20] = {0};
    printf("请输入购买的车次：");
    scanf("%s", trips);
    while(end != NULL)
    {
        if(!strcmp(end->tripsan.trip, trips))
        {
            //loctime结构体
            struct tm *info;
            time_t nowtime;
            time(&nowtime);
            info = localtime( &nowtime );
            time_t tim1, tim2;
            info->tm_year += 1900;
            tim1 = mktime(info);
            tim2 = mktime(&end->tripsan.time);
            if(tim1 <= tim2)
            {
                char strname[20] = {0};
                printf("请输入您的信息：\n");
                printf("用户名：");
                scanf("%s",strname); 
                
                while(end1 != NULL)
                {
                    if(!strcmp(strname, end1->pas.name))
                    {
                        //增加乘客车次信息
                        strcpy(end1->pas.passhead.trip, end->tripsan.trip);
                        end1->pas.passhead.time.tm_year = end->tripsan.time.tm_year;
                        end1->pas.passhead.time.tm_mon = end->tripsan.time.tm_mon;
                        end1->pas.passhead.time.tm_mday = end->tripsan.time.tm_mday;
                        end1->pas.passhead.time.tm_hour = end->tripsan.time.tm_hour;
                        end1->pas.passhead.time.tm_min = end->tripsan.time.tm_min;
                        strcpy(end1->pas.passhead.start, end->tripsan.start);
                        strcpy(end1->pas.passhead.destination, end->tripsan.destination);
                        end->tripsan.passengernum--;
                        printf("购买成功！\n");
                        logMessage(LOG_INFO, "passBuyTrips %s", end1->pas.name);
                        return 1;
                    }
                    end1 = end1->next;
                }
                if(end1 == NULL)
                {
                    printf("没有该用户！\n");
                    printf("请确认是否输入正确！\n");
                    logMessage(LOG_WARNING, "No pass");
                    return 0;
                }
            }
            else
            {
                printf("该车次的列车已出发！\n");
                logMessage(LOG_WARNING, "Trips is strat!");
                return 0;
            }

            
        }
        end = end->next;
    }
    if(end == NULL)
    {
        printf("没有该车次！\n");
        logMessage(LOG_ERROR, "No this Trips!");
    }

    return 0;
}

//改签               车次       乘客
int changeTicket(TRAIN *head, PASS *head1)
{
    TRAIN *end = head->next;
    PASS *end1 = head1->next;
    char str[20] = {0};
    printf("请输入需要改签的车次：");
    scanf("%s", str);

    if(!strcmp(str, end1->pas.passhead.trip))
    {
            char str1[20] = {0};
            printf("需要改签为：");
            scanf("%s", str1);
            if(!strcmp(str, str1))
            {
                printf("不能改签到该车次！\n");
                logMessage(LOG_WARNING, "No chenge same!");
                return 0;
            }
            
            int flg = 0;
            while(end != NULL)
            {
                if(!strcmp(str, end->tripsan.trip))
                {
                    end->tripsan.passengernum++;
                    flg++;
                }
                end = end->next; 
            }
            
            end = head->next;
            while(end != NULL)
            {
                if(!strcmp(str1, end->tripsan.trip))
                {
                    if(end->tripsan.passengernum > 0)
                    {
                        end->tripsan.passengernum--;
                        break;
                    }
                    else
                    {
                        printf("改签的车次没有余票！");
                        printf("改签失败！");
                        logMessage(LOG_ERROR, "Trips no ticket!");
                        return 0;
                    }
                    
                }
                end = end->next;
            }
    
            if(end != NULL)
            {
                strcpy(end1->pas.passhead.trip, end->tripsan.trip);
                end1->pas.passhead.time.tm_year = end->tripsan.time.tm_year;
                end1->pas.passhead.time.tm_mon = end->tripsan.time.tm_mon;
                end1->pas.passhead.time.tm_mday = end->tripsan.time.tm_mday;
                end1->pas.passhead.time.tm_hour = end->tripsan.time.tm_hour;
                end1->pas.passhead.time.tm_min = end->tripsan.time.tm_min;
                strcpy(end1->pas.passhead.start, end->tripsan.start);
                strcpy(end1->pas.passhead.destination, end->tripsan.destination);
                printf("改签成功！\n");
                logMessage(LOG_INFO, "passChangeTrips %s", end1->pas.name);
                return 1;
            }
            else
            {
                printf("改签失败！");
                logMessage(LOG_ERROR, "passChangepwd error!");
                return 0;
            }
    }
    else
    {
        printf("您没有购买这个车次的列车！\n");
        printf("请确认输入是否正确！\n");
        logMessage(LOG_WARNING, "pass no this trips!");
        return 0;
    }
}

//退票
int lossTicket(TRAIN *head, PASS *head1)
{
    TRAIN *end = head->next;
    PASS *end1 = head1->next;
    char str[20] = {0};
    printf("请输入需要退票的车次：");
    scanf("%s", str);
    while(end1 != NULL)
    {
        if(!strcmp(str, end1->pas.passhead.trip))
        {
            PASS *tmp = head1;
            //loctime结构体
            struct tm *info;
            time_t nowtime;
            time(&nowtime);
            info = localtime( &nowtime );
            time_t tim1, tim2;
            info->tm_year += 1900;
            tim1 = mktime(info);
            tim2 = mktime(&end->tripsan.time);
            if(tim1 <= tim2)
            {
                end->tripsan.passengernum++;
                memset(&end1->pas.passhead.time, 0, sizeof(struct tm));
                memset(&end1->pas.passhead, 0, sizeof(PASS));
                end1->pas.passhead.time.tm_mon -= 1;
                printf("退票成功！\n");
                logMessage(LOG_INFO, "passlossTrips %s", end1->pas.name);
                return 1;
            }
            else
            {
                printf("该车次已发车，无法退票！\n");
                printf("请前往车票改签！\n");
                logMessage(LOG_ERROR, "Trips is start!");
                return 0;
            }
        }
        end1 = end1->next;
    }
    if(end1 == NULL)
    {
        printf("您没有购买这个车次的列车！\n");
        printf("请确认输入是否正确！\n");
        logMessage(LOG_ERROR, "No this Trips!");
        return 0;
    }
}
